package smecta.client.print.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import org.springframework.stereotype.Controller;
import smecta.client.print.domain.ScpCodeFormModel;
import smecta.client.print.domain.ScpDataAcqModel;
import smecta.client.print.domain.ScpPackage;
import smecta.client.print.service.ScpCodeFormService;
import smecta.client.print.service.ScpDataAcqService;
import smecta.client.print.service.ScpProduceService;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

@Controller
public class CodeListPrintController {

    @FXML
    private CheckBox box1, box2, box3, box4, box5, box6, box7, box8, box9;
    @FXML
    private CheckBox column1, column2, column3, column4, column5, column6, column7;
    @FXML
    private Label title1,title2,title3,title4,title5,title6,title7,title8,title9;
    @FXML
    private Label titleValue1,titleValue2,titleValue3,titleValue4,titleValue5,titleValue6,titleValue7,titleValue8,titleValue9;
    @FXML
    private Label titleLabel;
    @FXML
    private TableView<ScpDataAcqModel> tableView ;
    @FXML
    private TextField size;
    @FXML
    private TextField height;
    @FXML
    private CheckBox isPrint;
    @FXML
    private Button closeButton;
    @FXML
    private TableColumn<ScpDataAcqModel, Integer> table1;
    @FXML
    private TableColumn<ScpDataAcqModel, String> table2;
    @FXML
    private TableColumn<ScpDataAcqModel, Integer> table3;
    @FXML
    private TableColumn<ScpDataAcqModel, BigDecimal> table4;
    @FXML
    private TableColumn<ScpDataAcqModel, Integer> table5;
    @FXML
    private TableColumn<ScpDataAcqModel, BigDecimal> table6;
    @FXML
    private TableColumn<ScpDataAcqModel, BigDecimal> table7;
    @FXML
    private TableColumn<ScpDataAcqModel, BigDecimal> table8;


    private Properties printSettings;

    private Map<CheckBox,String> titleMap = new LinkedHashMap<>();
    private List<CheckBox> columnCheckList = new ArrayList<>();
    private List<Label> titleList = new ArrayList<>();
    private List<Label> titleValueList = new ArrayList<>();

    private ScpCodeFormModel codeFormModel = new ScpCodeFormModel();
    private List<ScpDataAcqModel> boxList = new ArrayList<>();
    private ScpPackage produceModel = new ScpPackage();


    public ScpDataAcqService scpDataAcqService ;
    public ScpProduceService produceService;
    public ScpCodeFormService scpCodeFormService;
    @FXML
    public void initialize() {
        loadCodeSettings();
        initData();
        initTitleValue();
        initCodeTable();
    }

    public void initData(){
        String codeNum = "码单0a4f87ce-2145-4a64-8db9-6300e1976e31";
        codeFormModel = scpCodeFormService.findByCodeNum(codeNum);

        boxList = scpDataAcqService.getListByCodeNum(codeNum);
        ScpDataAcqModel scpDataAcqModel = boxList.get(0);

        produceModel = produceService.findById(scpDataAcqModel.getProduceId());

        table1.setCellValueFactory(new PropertyValueFactory<>("sn"));
        table2.setCellValueFactory(new PropertyValueFactory<>("machineNum"));
        table3.setCellValueFactory(new PropertyValueFactory<>("boxNum"));
        table4.setCellValueFactory(new PropertyValueFactory<>("boxWeight"));
        table5.setCellValueFactory(new PropertyValueFactory<>("canNum"));
        table6.setCellValueFactory(new PropertyValueFactory<>("canWeight"));
        table7.setCellValueFactory(new PropertyValueFactory<>("grossWeight"));
        table8.setCellValueFactory(new PropertyValueFactory<>("netWeight"));

        ObservableList<ScpDataAcqModel> produceTableData = FXCollections.observableArrayList(
                boxList
        );
        tableView.setItems(produceTableData);
    }



    public void initTitleValue(){
        ScpDataAcqModel scpDataAcqModel = boxList.get(0);

        titleMap = new LinkedHashMap<>();
        titleMap.put(box1,produceModel.getPinming());
        titleMap.put(box2,scpDataAcqModel.getDateDay());
        titleMap.put(box3,produceModel.getGuige());
        titleMap.put(box4,codeFormModel.getCodeNum());
        titleMap.put(box5,scpDataAcqModel.getClassNum());
        titleMap.put(box6,produceModel.getPihao());
        titleMap.put(box7,scpDataAcqModel.getLevelName());
        titleMap.put(box8,produceModel.getSeze());
        titleMap.put(box9,scpDataAcqModel.getTubeColor());

        titleList = new ArrayList<>();
        titleList.add(title1);
        titleList.add(title2);
        titleList.add(title3);
        titleList.add(title4);
        titleList.add(title5);
        titleList.add(title6);
        titleList.add(title7);
        titleList.add(title8);
        titleList.add(title9);

        titleValueList = new ArrayList<>();
        titleValueList.add(titleValue1);
        titleValueList.add(titleValue2);
        titleValueList.add(titleValue3);
        titleValueList.add(titleValue4);
        titleValueList.add(titleValue5);
        titleValueList.add(titleValue6);
        titleValueList.add(titleValue7);
        titleValueList.add(titleValue8);
        titleValueList.add(titleValue9);

        columnCheckList = new ArrayList<>();
        columnCheckList.add(column1);
        columnCheckList.add(column2);
        columnCheckList.add(column3);
        columnCheckList.add(column4);
        columnCheckList.add(column5);
        columnCheckList.add(column6);
        columnCheckList.add(column7);
    }

    public void initCodeTable(){

        for (Label label : titleList) {
            label.setText("");
        }
        for (Label label : titleValueList) {
            label.setText("");
        }

        AtomicInteger index = new AtomicInteger(0);
        titleMap.forEach((checkBox,titleValue)->{
            String id = checkBox.getId();
            if ("false".equals(printSettings.getProperty(id, "false"))){
                checkBox.setSelected(false);
            }else {
                checkBox.setSelected(true);
                Label needLabel = titleList.get(index.get());
                Label needValueLabel = titleValueList.get(index.get());

                needLabel.setText(checkBox.getText());
                needValueLabel.setText(titleValue);
                index.getAndIncrement();
            }
        });

        for (CheckBox column : columnCheckList) {
            String id = column.getId();
            if ("false".equals(printSettings.getProperty(id, "false"))){
                column.setSelected(false);
            }else {
                column.setSelected(true);
            }
        }

        Map<String,String> map = new HashMap<>();
        map.put("column1","table2");
        map.put("column2","table3");
        map.put("column3","table4");
        map.put("column4","table5");
        map.put("column5","table6");
        map.put("column6","table7");
        map.put("column7","table8");

        for(int i=0;i<columnCheckList.size();i++)
        {
            CheckBox column=columnCheckList.get(i);
            String id = column.getId();
            for (TableColumn col : tableView.getColumns()) {
                if(map.get(id).equalsIgnoreCase(col.getId()))
                {
                    col.setText(column.getText());
                    if ("false".equals(printSettings.getProperty(id, "false"))){
                        column.setSelected(false);
                        col.setPrefWidth(0.0d);
                        col.setMinWidth(0.0d);
                    }else {
                        column.setSelected(true);
                        col.setPrefWidth(120.0d);
                    }
                    break;
                }
            }
        }
        titleLabel.setText(printSettings.getProperty("title", "入库码单"));
        size.setText(printSettings.getProperty("size", "24"));
        height.setText(printSettings.getProperty("height", "1776.0"));
        if ("false".equals(printSettings.getProperty("isPrint", "false"))){
            isPrint.setSelected(false);
        }else {
            isPrint.setSelected(true);
        }

    }

    private void loadCodeSettings() {
        printSettings = new Properties();
        try (FileInputStream input = new FileInputStream("./codeSetting.ini")) {
            printSettings.load(input);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @FXML
    private void handleSaveProperties() {
        titleMap.forEach((titleBox,value)->{
            if (titleBox.isSelected()){
                printSettings.setProperty(titleBox.getId(),"true");
            }else {
                printSettings.setProperty(titleBox.getId(),"false");
            }
        });
        for (CheckBox checkBox : columnCheckList) {
            if (checkBox.isSelected()){
                printSettings.setProperty(checkBox.getId(),"true");
            }else {
                printSettings.setProperty(checkBox.getId(),"false");
            }
        }

        printSettings.setProperty("title",titleLabel.getText());
        printSettings.setProperty("size",size.getText());
        printSettings.setProperty("height",height.getText());
        printSettings.setProperty("isPrint",isPrint.getText());

        try (OutputStream output = new FileOutputStream("./codeSetting.ini")) {
            printSettings.store(output, null);
        } catch (IOException io) {
            io.printStackTrace();
        }

        loadCodeSettings();
        initCodeTable();
    }

    @FXML
    private void handleClose() {
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }



}
